package com.example.demo.security;

import com.example.demo.security.jwt.JWTAuthenticationFilter;
import com.example.demo.security.jwt.JWTLoginFilter;
import com.example.demo.security.jwt.JWTUtils;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Autowired
    UserService userService;


    @Autowired
    //加密方式
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //注入userDetailService

        auth.userDetailsService(userDetailsService)
                .passwordEncoder(bCryptPasswordEncoder);//加密方式
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .antMatchers(HttpMethod.GET,"/users/secret/**").permitAll()
                .antMatchers(HttpMethod.PUT,"/users/secret/**").permitAll()
                //.antMatchers(HttpMethod.GET,"/upload-dir/*").permitAll()
                .antMatchers(HttpMethod.GET,"/admin/**").hasRole("ADMIN")
                .antMatchers(HttpMethod.DELETE,"/admin/*/regions/**").hasRole("REGIONER")
                .antMatchers(HttpMethod.PUT,"/admin/*/regions/**").hasRole("REGIONER")
                .antMatchers(HttpMethod.DELETE,"/admin/**").hasRole("ULTIMATE")
                .antMatchers(HttpMethod.PUT,"/admin/**").hasRole("ULTIMATE")
                .antMatchers(HttpMethod.POST,"/admin/**").hasRole("ULTIMATE")
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthenticationFilter(authenticationManager()))
                // .formLogin().loginProcessingUrl("/login")
                //.successHandler(loginSuccessHandler);
                .addFilter(new JWTLoginFilter(authenticationManager()));

       /* http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()

                //.antMatchers(HttpMethod.GET,"/upload-dir/*").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthenticationFilter(authenticationManager()))
               // .formLogin().loginProcessingUrl("/login")
                //.successHandler(loginSuccessHandler);
                .addFilter(new JWTLoginFilter(authenticationManager()));*/
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Bean
    public JWTUtils jwtUtils()
    {
        return new JWTUtils();
    }

}